//
//  LBWEmoticonTools.swift
//  weibo
//
//  Created by Limonwei on 15/12/15.
//  Copyright © 2015年 heima. All rights reserved.
//

//表情工具类  其对应的view对应表情键盘上的collectionView

import UIKit

//一页表情有多少行
let LBWEmoticonsRowOfPage = 3

//一页表情有多少列
let LBWEmoticonsColOfPage = 7

//一页显示多少个表情数据
let LBWEmoticonNumOfPage = LBWEmoticonsRowOfPage * LBWEmoticonsColOfPage - 1

class LBWEmoticonTools: NSObject {
    
    //提供一个全局访问点
    static let shareTools: LBWEmoticonTools = LBWEmoticonTools()
    
    //表情数据的bundle文件
    lazy var emoticonBundle: NSBundle = {
       
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
        
    }()
    
    //最近使用的表情数据
    lazy var recentEmoticons: [LBWEmoticon] = {
    
        //解档
        var result = NSKeyedUnarchiver.unarchiveObjectWithFile(self.recentArchivePath) as? [LBWEmoticon]
        
        if result == nil {
            result = [LBWEmoticon]()
        }
        
        return result!

    }()
    
    //读取默认表情数据
    lazy var defaultEmoticons: [LBWEmoticon] = {
    
        return self.emoticonWithPath("default/info.plist")
 
    }()
    
    //读取emoji表情数据
    lazy var emojiEmoticons: [LBWEmoticon] = {
    
        return self.emoticonWithPath("emoji/info.plist")
    
    }()
    
    //读取浪小花表情数据
    lazy var lxhEmoticons: [LBWEmoticon] = {
    
        return self.emoticonWithPath("lxh/info.plist")
    
    }()
    
    //最近表情归档的路径
    private lazy var recentArchivePath: String = {
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first! as NSString).stringByAppendingPathComponent("recent.archive")
        return path
        
    }()
    
    
    //供表情键盘的collectionView使用的数据
    lazy var allEmoticons: [[[LBWEmoticon]]] = {
       
        return [
        
            self.sectionWithEmoticon(self.recentEmoticons),
            self.sectionWithEmoticon(self.defaultEmoticons),
            self.sectionWithEmoticon(self.emojiEmoticons),
            self.sectionWithEmoticon(self.lxhEmoticons)
        ]
        
    }()
    
    
    //供外界调用的方法
    
    //保存最近表情
    func saveRecent(emoticon: LBWEmoticon) {
    
        let array = recentEmoticons as NSArray
        
        let index = array.indexOfObject(emoticon)
        
        if index != NSNotFound {
            
            recentEmoticons.removeAtIndex(index)
        }
        
       //将当前表情模型添加到最近表情数组中
        recentEmoticons.insert(emoticon, atIndex: 0)
        
        //把超出20个的表情数据给干掉
        while recentEmoticons.count > 20 {
            recentEmoticons.removeLast()
        }
        
        //更新数据
        allEmoticons[0][0] = recentEmoticons
        
        //归档
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: recentArchivePath)
    
    }
    
    //通过表情字符串,找到表情模型
    func emoticonWithChs(chs: String) -> LBWEmoticon? {
        //谓词搜索
        let p = NSPredicate(format: "chs = %@", chs)
        
        var result = (defaultEmoticons as NSArray).filteredArrayUsingPredicate(p)
        
        if let first = result.first as? LBWEmoticon{
            return first
        }
        
        result = (lxhEmoticons as NSArray).filteredArrayUsingPredicate(p)
        
        if let first = result.first as? LBWEmoticon{
            return first
        }
        
        return nil
    }
    
    
    
    
    //每个section的表情
    private func sectionWithEmoticon(emoticons: [LBWEmoticon]) -> [[LBWEmoticon]] {
        
        //取到当前表情一共有多少页
        let pageCount = (emoticons.count - 1) / LBWEmoticonNumOfPage + 1
        
        //定义一个返回的数组
        var result = [[LBWEmoticon]]()
        
        //遍历截取每一页显示的数据
        for i in 0..<pageCount {
            //计算截取的范围
            let location = i * LBWEmoticonNumOfPage
            var length = LBWEmoticonNumOfPage
            
            // 如果开始截取位置加上截取长度大于数组的个数,那么就代表越界
            if location + length > emoticons.count {
                //调整length
                length = emoticons.count - location
            }
            
            let range = NSMakeRange(location, length)
            
            //截取子数组
            let subArray = (emoticons as NSArray).subarrayWithRange(range) as! [LBWEmoticon]
            
            //添加到返回数组
            result.append(subArray)
        }
        return result
        
    }
    
    //读取表情数据
    
    private func emoticonWithPath(path: String) ->[LBWEmoticon] {
        let file = self.emoticonBundle.pathForResource(path, ofType: nil)!
        
        //读取plist文件的内容
        let array = NSArray(contentsOfFile: file)!
        
        var result = [LBWEmoticon]()
        
        for value in array {
            //判断是否是字典
            if let dict  = value as? [String: AnyObject]{
                
                let emoticon = LBWEmoticon(dict: dict)
                
                emoticon.path = (file as NSString).stringByDeletingLastPathComponent
                
                result.append(emoticon)
            }
        }
        return result
    }
}

































